<template>
  <div class="login-container">
    <div class="header">
      <div class="container clearfix">
        <router-link to="/">
          <img src="@/assets/logo.svg" alt="" />
          <img src="@/assets/logo-text.png" alt="" />
        </router-link>
        <span>登录</span>

        <div class="float-right">
          <ul class="enter-con clearfix">
            <li>
              如有疑问？
              <a href="javascript:;">
                <img src="@/assets/login/service.svg" alt="" /> 在线客服
              </a>
            </li>
            <li>
              已有账号？
              <router-link to="/login">
                <a href="javascript:;">去登录 》</a>
              </router-link>
            </li>
          </ul>
        </div>
      </div>
    </div>

    <div class="form-container">
      <div class="container">
        <el-form
          ref="loginForm"
          :model="loginForm"
          :rules="loginRules"
          class="login-form"
          autocomplete="on"
          label-position="left"
        >
          <div class="title-container">
            <h3 class="title">账户登录</h3>
          </div>

          <div style="padding: 30px 20px">
            <el-form-item prop="username">
              <span class="svg-container">
                <svg-icon icon-class="user" />
              </span>
              <el-input
                ref="username"
                v-model="loginForm.username"
                placeholder="用户名"
                name="username"
                type="text"
                tabindex="1"
                autocomplete="on"
              />
            </el-form-item>

            <el-tooltip
              v-model="capsTooltip"
              content="Caps lock is On"
              placement="right"
              manual
            >
              <el-form-item prop="password">
                <span class="svg-container">
                  <svg-icon icon-class="password" />
                </span>
                <el-input
                  :key="passwordType"
                  ref="password"
                  v-model="loginForm.password"
                  :type="passwordType"
                  placeholder="密码"
                  name="password"
                  tabindex="2"
                  autocomplete="on"
                  @keyup.native="checkCapslock"
                  @blur="capsTooltip = false"
                  @keyup.enter.native="handleLogin"
                />
                <span class="show-pwd" @click="showPwd">
                  <svg-icon
                    :icon-class="
                      passwordType === 'password' ? 'eye' : 'eye-open'
                    "
                  />
                </span>
              </el-form-item>
            </el-tooltip>
            <el-button
              :loading="loading"
              type="primary"
              style="width: 100%"
              size="large"
              @click.native.prevent="handleLogin"
              >登录
            </el-button>
          </div>
        </el-form>
      </div>
    </div>

    <div class="footer">
      <div class="container">
        Copyright 2017 叮当（天津）电子商务有限公司 All Rights Reserved
        网站备案号：津ICP备17003187号-1
      </div>
    </div>
  </div>
</template>

<script>
import { validUsername } from "@/utils/validate";
import SocialSign from "./components/SocialSignin";

export default {
  name: "Login",
  components: { SocialSign },
  data() {
    const validateUsername = (rule, value, callback) => {
      // if (!validUsername(value)) {
      //   callback(new Error("Please enter the correct user name"));
      // } else {
      //   callback();
      // }
      callback();
    };
    const validatePassword = (rule, value, callback) => {
      // if (value.length < 6) {
      //   callback(new Error("The password can not be less than 6 digits"));
      // } else {
      //   callback();
      // }
      callback();
    };
    return {
      loginForm: {
        username: "",
        password: "",
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", validator: validateUsername },
        ],
        password: [
          { required: true, trigger: "blur", validator: validatePassword },
        ],
      },
      passwordType: "password",
      capsTooltip: false,
      loading: false,
      showDialog: false,
      redirect: undefined,
      otherQuery: {},
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        const query = route.query;
        if (query) {
          this.redirect = query.redirect;
          this.otherQuery = this.getOtherQuery(query);
        }
      },
      immediate: true,
    },
  },
  created() {
    // window.addEventListener('storage', this.afterQRScan)
    this.$notify({
      title: "登录账户信息",
      dangerouslyUseHTMLString: true,
      duration: 5000,
      message:
        "<p>用户名(采购商)：purchaser <br> 密码：任意</p><p>用户名(供应商)：supplier <br> 密码：任意</p><p>用户名(招标办)：bidder <br> 密码：任意</p><p>用户名(评标专家)：expert <br> 密码：任意</p>",
    });
  },
  mounted() {
    if (this.loginForm.username === "") {
      this.$refs.username.focus();
    } else if (this.loginForm.password === "") {
      this.$refs.password.focus();
    }
  },
  destroyed() {
    // window.removeEventListener('storage', this.afterQRScan)
  },
  methods: {
    checkCapslock(e) {
      const { key } = e;
      this.capsTooltip = key && key.length === 1 && key >= "A" && key <= "Z";
    },
    showPwd() {
      if (this.passwordType === "password") {
        this.passwordType = "";
      } else {
        this.passwordType = "password";
      }
      this.$nextTick(() => {
        this.$refs.password.focus();
      });
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$store
            .dispatch("user/login", this.loginForm)
            .then(() => {
              this.$router.push({
                path: this.redirect || "/dashboard",
                query: this.otherQuery,
              });
              this.loading = false;
            })
            .catch(() => {
              this.loading = false;
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    getOtherQuery(query) {
      return Object.keys(query).reduce((acc, cur) => {
        if (cur !== "redirect") {
          acc[cur] = query[cur];
        }
        return acc;
      }, {});
    },
  },
};
</script>

<style lang="scss" scope>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #498cd0;
.login-container {
  .header {
    padding: 20px 0;
    .container {
      width: 1200px;
      margin: 0 auto;

      img {
        width: 120px;
        height: 66px;

        & + img {
          height: 52px;
          transform: translate(10px, 6px);
        }
      }
      span {
        display: inline-block;
        font-size: 22px;
        color: #333;
        padding-left: 30px;
        margin-left: 40px;
        border-left: 2px solid #aaa;
        transform: translateY(-18px);
      }
    }
    .enter-con {
      padding-top: 40px;
      margin: 0;
      list-style: none;
      img {
        width: 25px;
        height: auto;
        vertical-align: middle;
      }
      > li {
        float: left;
        padding: 0 15px;
        line-height: 23px;

        > a {
          color: #3982cd;
        }
        & + li {
          margin-right: 0;
          position: relative;
        }
        & + li:after {
          content: "";
          display: block;
          width: 1px;
          height: 15px;
          background: #aaa;
          position: absolute;
          left: 0;
          top: 50%;
          transform: translateY(-50%);
        }
      }
    }
  }

  .form-container {
    background: url(../../assets/login/bg.jpg) no-repeat center bottom;
    background-size: cover;

    .container {
      width: 1200px;
      min-height: 700px;
      margin: 0 auto;
      position: relative;
      .login-form {
        width: 360px;
        padding-bottom: 30px;
        background-color: #fff;
        position: absolute;
        right: 0;
        top: 50%;
        transform: translateY(-50%);
      }
    }
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;
    background: #f2f2f2;
    padding: 20px;

    .title {
      font-size: 20px;
      color: $light_gray;
      margin: 0;
      font-weight: normal;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }

  .thirdparty-button {
    position: absolute;
    right: 0;
    bottom: 6px;
  }

  @media only screen and (max-width: 470px) {
    .thirdparty-button {
      display: none;
    }
  }

  .footer {
    .container {
      width: 1200px;
      margin: 0 auto;
    }
    background: #002a39;
    padding: 15px 0;
    color: #667f88;
    font-size: 12px;
  }
}
</style>


<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: rgb(32, 25, 25);

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    // height: 30px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      height: 22px;
      padding: 16px 5px 16px 15px;
      box-sizing: border-box;
      font-size: 14px;
      //color: $light_gray;
      //height: 22px;
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
    margin-bottom: 30px;
  }
}
</style>
